var express = require('express');
var router = express.Router();
var path = require('path');

var homeTrl = require('../controllers/home-trl');
var newsTrl = require('../controllers/news-trl');
var planeTrl = require('../controllers/plane-trl');
var searchTrl = require('../controllers/search-trl');
var queryTrl = require('../controllers/query-trl');
var aboutTrl = require('../controllers/about-trl');
var adminTrl = require('../controllers/admin-trl');
var adminHomeBannerTrl = require('../controllers/admin-home-banner-trl');
var adminHomePromotionTrl = require('../controllers/admin-home-promotion-trl');
var adminNewsCrawler = require('../controllers/admin-news-crawler-trl');
var adminNewsArticleTrl = require('../controllers/admin-news-article-trl');
var adminPlaneTrl = require('../controllers/admin-plane-trl');
var adminPlaneCrawler = require('../controllers/admin-plane-crawler-trl');
var adminSearchTrl = require('../controllers/admin-search-trl');
var adminUserTrl = require('../controllers/admin-user-trl');
var adminChatTrl = require('../controllers/admin-chat-trl');
var adminPlaneArticleTrl = require('../controllers/admin-plane-article-trl');

// statics
router.use('/assets', express.static(path.join(process.cwd(), 'public')));
router.use('/vendor', express.static(path.join(process.cwd(), 'bower_components')));

//controllers
router.get('/', homeTrl.home);

router.get('/news/list', newsTrl.list);
router.get('/news/article', newsTrl.newsArticle);

router.get('/plane/list', planeTrl.list);
router.get('/plane/detail', planeTrl.detail);
router.get('/plane/article', planeTrl.planeArticle);

router.get('/news/search', searchTrl.newsSearch);
router.get('/plane/search', searchTrl.planeSearch);

router.get('/query', queryTrl.show);
router.post('/query', queryTrl.postData);

router.get('/about', aboutTrl.about);


//admin controllers
router.all('/admin', function(req, res, next) {
  if (!req.session.user || !req.session.user._id) {
    return res.redirect('/login');
  }
  next();
});

router.get('/admin', adminTrl.admin);
router.get('/login', adminTrl.login);
router.post('/login', adminTrl.checkLogin);
router.get('/logout', adminTrl.logout);

router.post('/admin/homeBanner', adminHomeBannerTrl.showBanner);
router.post('/admin/saveBanner', adminHomeBannerTrl.saveBanner);
router.post('/admin/updateBanner', adminHomeBannerTrl.updateBanner);
router.post('/admin/removeBanner', adminHomeBannerTrl.removeBanner);

router.post('/admin/homePromotion', adminHomePromotionTrl.showPromotion);
router.post('/admin/savePromotion', adminHomePromotionTrl.savePromotion);
router.post('/admin/updatePromotion', adminHomePromotionTrl.updatePromotion);
router.post('/admin/removePromotion', adminHomePromotionTrl.removePromotion);

router.post('/admin/crawlerNews', adminNewsCrawler.showPage);
router.post('/admin/crawlerOneNews', adminNewsCrawler.upadteOne);
router.post('/admin/crawlerManyNews', adminNewsCrawler.updateMany);

router.post('/admin/newsArticleList', adminNewsArticleTrl.newsList);
router.post('/admin/newsListHasPhoto', adminNewsArticleTrl.newsListHasPhoto);
router.post('/admin/showNewsArticle', adminNewsArticleTrl.showArticle);
router.post('/admin/saveNewsArticle', adminNewsArticleTrl.saveArticle);
router.post('/admin/updateNewsArticle', adminNewsArticleTrl.updateArticle);
router.post('/admin/removeNewsArticle', adminNewsArticleTrl.removeArticle);

router.post('/admin/crawlerPlane', adminPlaneCrawler.showPage);
router.post('/admin/crawlerOnePlane', adminPlaneCrawler.upadteOne);
router.post('/admin/crawlerManyPlane', adminPlaneCrawler.updateMany);

router.post('/admin/planeList', adminPlaneTrl.planeList);
router.post('/admin/showPlane', adminPlaneTrl.showPlane);
router.post('/admin/savePlane', adminPlaneTrl.savePlane);
router.post('/admin/updatePlane', adminPlaneTrl.updatePlane);
router.post('/admin/removePlane', adminPlaneTrl.removePlane);

router.post('/admin/planeArticleList', adminPlaneArticleTrl.planeList);
router.post('/admin/planeListChoose', adminPlaneArticleTrl.planeListChoose);
router.post('/admin/showPlaneArticle', adminPlaneArticleTrl.showArticle);
router.post('/admin/savePlaneArticle', adminPlaneArticleTrl.saveArticle);
router.post('/admin/updatePlaneArticle', adminPlaneArticleTrl.updateArticle);
router.post('/admin/removePlaneArticle', adminPlaneArticleTrl.removeArticle);

router.post('/admin/newsEditSearch', adminSearchTrl.newsEditSearch);
router.post('/admin/newsChooseSearch', adminSearchTrl.newsChooseSearch);
router.post('/admin/planeSearch', adminSearchTrl.planeSearch);
router.post('/admin/planeEditSearch', adminSearchTrl.planeEditSearch);
router.post('/admin/planeChooseSearch', adminSearchTrl.planeChooseSearch);

router.post('/admin/showChatDay', adminChatTrl.showChatDay);
router.post('/admin/showChatMonth', adminChatTrl.showChatMonth);
router.post('/admin/showChatYear', adminChatTrl.showChatYear);
router.post('/admin/showChatKeyword', adminChatTrl.showChatKeyword);

router.all('/admin/user', function(req, res, next) {
  if (!req.session.user || !req.session.user._id || req.session.user.typeName != '系统管理员') {
    return res.redirect('/login');
  }
  next();
});

router.post('/admin/userList', adminUserTrl.userList);
router.post('/admin/userShow', adminUserTrl.showUser);
router.post('/admin/userSave', adminUserTrl.saveUser);
router.post('/admin/updateUser', adminUserTrl.updateUser);
router.post('/admin/userRemove', adminUserTrl.removeUser);

router.use(function(req, res) {
  res.render("404");
});

module.exports = router;
